﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Announcement.Model;
using Announcement.DAL.DataSetGarageTableAdapters;
using Announcement.DAL.DataSetGarageDetailsTableAdapters;
using Announcement.DAL.DataSetApplyTableAdapters;

namespace Announcement
{
    public partial class Sell : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ApplyTable at = (ApplyTable)Session["ApplyTable"];
                GarageTable gt = (GarageTable)Session["GarageTable"];

                string cCode = at.ContractCode;
                string districtName = gt.DistrictName;
                string developersName = gt.DevelopersName;
                DateTime startDate = (DateTime)gt.StartDate;
                DateTime endDate = (DateTime)gt.EndDate;
                DateTime fillDate = (DateTime)gt.FillDate;
                string houseSoldRate = gt.HouseSoldRate.ToString();
                string garageSold = gt.GarageSold.ToString();
                string garageUnsold = gt.GarageUnsold.ToString();

                labCCode.Text = cCode;
                labApplyDate.Text = fillDate.ToString("yyyy年MM月dd日");
                labDistrictName.Text = districtName;
                labDistrictName2.Text = districtName;
                labDevelopersName.Text = developersName;
                labStartDate.Text = startDate.ToString("yyyy年MM月dd日");
                labEndDate.Text = endDate.ToString("yyyy年MM月dd日");
                labHouseSoldRate.Text = houseSoldRate;
                labGarageSold.Text = garageSold;
                labGarageUnsold.Text = garageUnsold;
                labFillDate.Text = fillDate.ToString("yyyy年MM月dd日");

                if (Request.QueryString["isPrn"] == "true")
                {
                    btnSave.Visible = false;
                    btnPrn.Enabled = true;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            btnPrn.Enabled = true;
            btnSave.Enabled = false;

            //保存申请表到数据库
            ApplyTable at = (ApplyTable)Session["ApplyTable"];
            var applyRow = new T_ApplyTableTableAdapter();

            //如果重复保存则返回
            if (applyRow.GetDataByContractCode(at.ContractCode).Rows.Count > 0)
            {
                ClientScript.RegisterClientScriptBlock(typeof(Page), "已存在", @"alert('" + "该公告已存在,无需重复保存" + "');", true);
                return;
            }

            applyRow.Insert(at.ContractCode, at.Applicant, at.Legal, at.CardTpye,
                at.CardNum, at.Agents, at.Phone, at.CardType2, at.CardNum2,
                at.HouseLocated, at.HouseNum, at.ApplyType, at.ApplyDate, at.IsPay,at.Money);

            //保存车位销售方案到数据库
            GarageTable gt = (GarageTable)Session["GarageTable"];
            var garageRow = new T_GarageTableAdapter();

            int garageId = Convert.ToInt32( garageRow.InsertGetId(at.ContractCode,gt.HouseSum, gt.HouseSold, gt.HouseUnsold, gt.HouseSoldRate, gt.HouseIsSell, gt.GarageSum,
                gt.GarageSold, gt.GarageUnsold, gt.GaragePlanSell, gt.GaragePlanRate, gt.DistrictName, gt.DevelopersName,
                gt.StartDate, gt.EndDate, gt.FillDate));


            //保存车位销售方案扩展数据到数据库
            Garage_Details gd1 = (Garage_Details)Session["Garage_Details1"];
            Garage_Details gd2 = (Garage_Details)Session["Garage_Details2"];
            Garage_Details gd3 = (Garage_Details)Session["Garage_Details3"];
            Garage_Details gd4 = (Garage_Details)Session["Garage_Details4"];
            gd1.Garage_Id = garageId;
            var detailsRow = new T_Garage_DetailsTableAdapter();
            detailsRow.Insert(gd1.Garage_Id, gd1.Number, Convert.ToDouble(gd1.Area), gd1.UnitPrice, gd1.TotalPrice);

            if (gd2 != null)
            {
                gd2.Garage_Id = garageId;
                detailsRow.Insert(gd2.Garage_Id, gd2.Number, Convert.ToDouble(gd2.Area), gd2.UnitPrice, gd2.TotalPrice);
            }
            if (gd3 != null)
            {
                gd3.Garage_Id = garageId;
                detailsRow.Insert(gd3.Garage_Id, gd3.Number, Convert.ToDouble(gd3.Area), gd3.UnitPrice, gd3.TotalPrice);
            }
            if (gd4 != null)
            {
                gd4.Garage_Id = garageId;
                detailsRow.Insert(gd4.Garage_Id, gd4.Number, Convert.ToDouble(gd4.Area), gd4.UnitPrice, gd4.TotalPrice);
            }
            
        }

        protected void lbtnExit_Click(object sender, EventArgs e)
        {
            Session["AdminUserName"] = null;
            Session["UserName"] = null;
            Response.Redirect("~/Login.aspx");
        }
    }
}
